package com.ihelp.rest.mis.platform.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ihelp.rest.mis.platform.dao.SiteDao;
import com.ihelp.rest.mis.platform.dao.SiteTypeDao;
import com.ihelp.rest.mis.pojo.Site;
import com.ihelp.rest.mis.pojo.SiteType;
import com.ihelp.rest.utils.JsonCommon;

@Service
public class SiteService {

	@Autowired
	private SiteTypeDao stDao;
	
	@Autowired
	private SiteDao siteDao;
	
	/**
	 * 查询网站分类Page 返回Json串
	 * @param queryCondition
	 * @param start
	 * @param pageSize
	 * @param sort
	 * @param order
	 * @return
	 */
	public String findSiteTypePage(SiteType queryCondition,int start,int pageSize,String sort, String order){
		return JsonCommon.getGridJson(this.stDao.querySiteTypePage(queryCondition,start,pageSize,sort,order)).toString();
	}
	
	public String addSiteType(SiteType st){
		Map<String ,Object >  result = new HashMap<String, Object>();
		if(StringUtils.isBlank(st.getId())){
			st.setId(null);
		}
		try{
			this.stDao.saveOrUpdate(st);
			result.put("success", "true");
			result.put("data", "网站分类保存成功");
		}catch(Exception e){
			result.put("success", "false");
			result.put("data", "网站分类保存失败"+e.getMessage());
		}
		return JSONObject.fromObject(result).toString() ;
	}
	
	public String getSiteTypeByPId(String pid){
		return JSONArray.fromObject(this.stDao.querySiteTypeByPid(pid)).toString();
	}
	
	public SiteType getSiteTypeById(String id){
		return this.stDao.get(id);
	}
	
	public SiteType getSiteTypeByCode(String id){
		return (SiteType) this.stDao.querySiteTypeByCode(id);
	}
	
	public String findSiteTypeTree(String parentcode){
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		if(StringUtils.isBlank(parentcode)){
			List<SiteType> rootResult = this.stDao.querySiteTypeByPid("0");
			sb.append("{");
			sb.append("\"id\":\"0\",");
			sb.append("\"text\":\"网站分类\"");
			//sb.append("\"attributes\":{\"id\":\"0\"},");
			if (null != rootResult && rootResult.size() > 0) {
				sb.append(",\"state\":\"closed\"");
			}
			sb.append("}");
		}else{
			List<SiteType> result = this.stDao.querySiteTypeByPid(parentcode);
			for (SiteType one : result) {
				boolean haveSub = false;
				List<SiteType> subResult = this.stDao.querySiteTypeByPid(one.getTypecode());
				if (null != subResult && subResult.size() > 0) {
					haveSub = true;
				}
				sb.append("{");
				sb.append("\"id\":\"").append(one.getTypecode()).append("\",");
				sb.append("\"text\":\"").append(one.getTypename()).append("\"");
				//sb.append("\"attributes\":{\"id\":\"").append(one.getTypecode()).append("\"},");
				if (haveSub) {
					sb.append(",\"state\":\"closed\"");
				}
				sb.append("}");
			}
		}
		sb.append("]");
		sb = new StringBuilder(sb.toString().replace("}{", "},{"));
		return sb.toString();
	}
	
	public String getNextCode(String pid){
		return this.stDao.getNewCode(pid);
	}
	
	
	public String delSiteType(String id){
		Map<String ,Object >  result = new HashMap<String, Object>();
		try{
			this.stDao.delete(id);
			result.put("success", "true");
			result.put("data", "网站分类删除成功");
		}catch(Exception e){
			result.put("success", "false");
			result.put("data", "网站分类删除失败"+e.getMessage());
		}
		return JSONObject.fromObject(result).toString() ;
	}
	
	
	public String findPageAllSites(int start, int pageSize,String typecode) {
		return JsonCommon.getGridJson(this.siteDao.getAllSiteListSize(typecode),
				this.siteDao.getAllSiteList(start, pageSize,typecode)).toString();
	}
	
	public String saveSite(Site site){
		Map<String ,Object >  result = new HashMap<String, Object>();
		if(StringUtils.isBlank(site.getId())){
			site.setId(null);
			site.setAddtime((new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date()).toString());
		}
		try{
			this.siteDao.saveOrUpdate(site);
			result.put("success", "true");
			result.put("data", "网站保存成功");
		}catch(Exception e){
			result.put("success", "false");
			result.put("data", "网站保存失败"+e.getMessage());
		}
		return JSONObject.fromObject(result).toString() ;
	}
	
	
	public String delSite(String id){
		Map<String ,Object >  result = new HashMap<String, Object>();
		try{
			this.siteDao.delete(id);
			result.put("success", "true");
			result.put("data", "网站删除成功");
		}catch(Exception e){
			result.put("success", "false");
			result.put("data", "网站删除失败"+e.getMessage());
		}
		return JSONObject.fromObject(result).toString() ;
	}
	
	
	public Site getSiteById(String id){
		return this.siteDao.get(id);
	}
}
